import 'package:account/bean/user.dart';
import 'package:account/utils/cache.dart';
import 'package:flutter/material.dart';


class PwdApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PwdState();
  }
}

class PwdState extends State {
  @override
  Widget build(BuildContext context) {
    //当前密码缓存
    bool currentPwdVisible = false;
    //没有密码的显示
    String title = "密码设置";
    //有密码的显示
    if (Cache.user!.pwd != null) {
      title = "修改密码";
      currentPwdVisible = true;
    }

    TextEditingController conOldPwd = TextEditingController();
    TextEditingController conNewPwd = TextEditingController();
    TextEditingController conNewConform = TextEditingController();
    TextEditingController conTip = TextEditingController();
    GlobalKey globalKey = new GlobalKey<FormState>();

    return Scaffold(
        appBar: AppBar(
          title: Text(title),
          leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () {
                Navigator.of(context).pop(1);
              }), //换成iconButton
          actions: [
            GestureDetector(
                onTap: () {
                  //如果当前已有密码，则判断当前密码是否相同
                  // if (user.pwd != null) {
                  //   if (user.pwd != conOldPwd.text) {}
                  // }
                  if ((globalKey.currentState as FormState).validate()) {
                    Cache.setUser(User(conNewPwd.text, conTip.text));
                    Navigator.of(context).pop(1);
                  }
                },
                child: Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.only(right: 20),
                    child: Text(
                      "保存",
                      style: TextStyle(fontSize: 20),
                    )))
          ],
        ),
        body: Container(
            padding: EdgeInsets.only(left: 30, right: 30),
            child: Form(
              key: globalKey,
              autovalidateMode: AutovalidateMode.always,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Visibility(
                      visible: currentPwdVisible,
                      child: TextFormField(
                        controller: conOldPwd,
                        decoration: InputDecoration(hintText: "请输入现在的密码"),
                        validator: (v) {
                          if (Cache.user!.pwd == null) {
                            return null;
                          }
                          return (Cache.user!.pwd == conOldPwd.text &&
                                  conOldPwd.text.length > 3)
                              ? null
                              : "当前密码错误";
                        },
                      )),
                  TextFormField(
                    controller: conNewPwd,
                    decoration: InputDecoration(hintText: "请输入新的密码"),
                    validator: (v) {
                      if(v!.length < 6)
                        return "密码最少需要6位";
                      return  conOldPwd.text != v ? null : "新旧密码不能相同";
                    },
                  ),
                  TextFormField(
                    controller: conNewConform,
                    decoration: InputDecoration(hintText: "请再次输入新密码"),
                    validator: (v) {
                      return conNewPwd.text == v ? null : "两次填写的密码不一致";
                    },
                  ),
                  TextFormField(
                    controller: conTip,
                    decoration: InputDecoration(hintText: "请输入密码提示"),
                    validator: (v) {
                      return v!.length >= 1 ? null : "请输入密码提示";
                    },
                  ),
                ],
              ),
            )));
  }
}
